package com.test.frameworkdemo

import android.databinding.ViewDataBinding
import android.support.v7.widget.LinearLayoutManager
import android.widget.Toast
import com.test.frameworkdemo.databinding.ActivityMainBinding
import com.yanzhenjie.nohttp.rest.Response
import com.yanzhenjie.nohttp.rest.StringRequest
import com.yoofn.framework.activity.BaseActivity
import com.yoofn.framework.adapter.DiffCallBack
import com.yoofn.framework.adapter.MultiTypeAdapter
import com.yoofn.framework.config.ViewConfig
import com.yoofn.framework.http.HttpNetData
import com.yoofn.framework.item.BaseItem
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity<ActivityMainBinding>() {

    override fun setupConfig(): ViewConfig {
        return ViewConfig(R.layout.activity_main).apply {
            shouldSwipeRefresh = true
        }
    }

    override fun initView() {
        srl_list.run {
            netFailedAction = {btn_net_failed.performClick()}
            recyclerView.run {
                layoutManager = LinearLayoutManager(context)
                adapter = MultiTypeAdapter(this@MainActivity, srl_list)
            }
        }
    }

    override fun initClick() {
        btn_refresh.setOnClickListener {
            Toast.makeText(this@MainActivity, "刷新", Toast.LENGTH_LONG).show()
            val arrayList = (0..20).mapTo(ArrayList<DemoItem>()) { DemoItem(srl_list.recyclerView).apply { bean = DemoItem.DemoBean().apply { name = "20" } } }
            srl_list.run {
                recyclerView.run {
                    (adapter as MultiTypeAdapter).let {
                        it.notifyDiff(object : DiffCallBack(it.getItems(), arrayList) {
                            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                                return true
                            }

                            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                                return false
                            }
                        })
                    }
                }
            }
        }

        btn_empty_items.setOnClickListener {

            srl_list.run {
                recyclerView.run {
                    (adapter as MultiTypeAdapter).let {
                        it.notifyDiff(object : DiffCallBack(it.getItems(), ArrayList<BaseItem<out ViewDataBinding>>()) {
                            override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                                return true
                            }

                            override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
                                return false
                            }
                        })
                    }
                }
            }
        }

        btn_net_failed.setOnClickListener {
            addRequest(HttpNetData(0, StringRequest("http://11.43.34.230:afds")))
        }
    }

    override fun doBusiness(what: Int, response: Response<String>) {

    }

    override fun onRequestFailed(what: Int, response: Response<String>?) {
        with(contentView) {
            srl_list.run {
                srlList.onNetFailed()
            }
        }
    }
}
